package com.hk.service.impl;

import com.hk.dao.IOnlineMessageDao;
import com.hk.dao.impl.OnlineMessageDaoImpl;
import com.hk.model.OnlineMessage;
import com.hk.service.IOnlineMessageService;
import com.hk.utils.DateUtils;
import com.hk.utils.JdbcMysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: KangWang
 * Date: 2021/5/20 16:09
 * State：在线留言业务逻辑层
 */
public class OnlineMessageServiceImpl implements IOnlineMessageService {

    IOnlineMessageDao iOnlineMessageDao = new OnlineMessageDaoImpl();

    @Override
    public List<OnlineMessage> findAll(Integer page, Integer limit) {
        ResultSet resultSet = iOnlineMessageDao.findAll(page,limit);
        List<OnlineMessage> onlineMessageList = transverter(resultSet);
//        关闭资源
        JdbcMysql.jdbcClose();
        return onlineMessageList;
    }

    @Override
    public int count(Integer page, Integer limit) {
        ResultSet resultSet = iOnlineMessageDao.count(page,limit);
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
            return count;
        } catch (SQLException e) {
            e.getMessage();
        }
        //        关闭资源
        JdbcMysql.jdbcClose();
        return count;
    }

    @Override
    public OnlineMessage findById(Integer id) {
        ResultSet resultSet = iOnlineMessageDao.findById(id);
        List<OnlineMessage> onlineMessageList = transverter(resultSet);
        //        关闭资源
        JdbcMysql.jdbcClose();
        return onlineMessageList.get(0);
    }

    @Override
    public int save(OnlineMessage t) {
        String dateTimeString = DateUtils.dateTimeString(new Date());
        t.setGmtCreate(dateTimeString);
        t.setGmtModified(dateTimeString);
        int save = iOnlineMessageDao.save(t);
        if (save == 1) {
            System.out.println("留言信息添加成功！");
        }
        return save;
    }

    @Override
    public int update(OnlineMessage t) {
        return 0;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    /**
     * 将传递的数据进行转换成集合
     * @param resultSet
     * @return
     */
    public List<OnlineMessage> transverter(ResultSet resultSet ) {
        List<OnlineMessage> onlineMessageList = new ArrayList<>();
        try {
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String title = resultSet.getString("title");
                String email = resultSet.getString("email");
                String content = resultSet.getString("content");
                String gmtCreate = resultSet.getString("gmt_create");
                String gmtModified = resultSet.getString("gmt_modified");
//                封装对象
                OnlineMessage onlineMessage = new OnlineMessage(id,title,email,content,gmtCreate,gmtModified);
                onlineMessageList.add(onlineMessage);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return onlineMessageList;

    }

}
